﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Tradex.ComponentModel {

    /// <summary>
    /// This helper method implements extension methods helping classes to deal with the INotifyPropertyChanged interface.
    /// </summary>
    public static class INotifyPropertyChangedHelper {

        /// <summary>
        /// Extension OnPropertyChanged method. Smart enough to only throw the event if the two classes passed in are not equal,
        /// as otherwise the set is an obvious non-operation.
        /// 
        /// Takes a delegate to the event as parameter (as the event is otherwise not reachable from the interface) and the name of the
        /// property (as retrieving it would be slow).
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sender"></param>
        /// <param name="handler"></param>
        /// <param name="property"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1045:DoNotPassTypesByReference", MessageId = "3#")]
        public static void OnPropertyChanged<T>(this INotifyPropertyChanged sender, PropertyChangedEventHandler handler, string property, ref T oldValue, T newValue) {
            if (!System.Object.Equals(oldValue, newValue)) {
                oldValue = newValue;
                if (handler != null) {
                    handler(sender, new PropertyChangedEventArgs(property));
                }
            }
            return;
        }

    }

}
